OPC Studio User's Guide and Reference
OPC UA File Streams
Client and Subscriber Development > Features > Specialized Client Objects > OPC UA File Transfer > OPC UA File Streams
In This Topic

Stream is a form of abstraction for a sequence of bytes. Microsoft .NET libraries provide this abstraction by a Stream Class. Various concrete stream implementations exist, such as for operating system files, data in memory, network sockets, etc. Since the OPC UA files are also such sequences of bytes with all their typical properties, it makes sense to reuse the stream abstraction and provide a stream implementation base on OPC UA files.

Methods related to OPC UA file streams are all located in the IEasyUAFileTransferExtension2 Class. There is also an internal class inside OPC Data Client that implements the stream on an OPC UA file, but your code will never create an instance of that class directly. Instead, there are extension methods to open or create a stream, and by calling them you will obtain a stream object which behaves like any other stream in the .NET world. 

The features discussed here, or some of them, may not be available in all editions of the product. Check the Product Editions page for differences between the editions. The trial license has all features enabled (and is limited in period for which it provides valid data), but licenses for specific commercial editions may have functionality limitations.
This functionality is not available under (or the text does not apply to) COM development platform.

This article describes API level 2++ for OPC UA file transfer; for an overview of API levels, see OPC UA File Transfer Client.

You may also want to look at OPC UA File Transfers internals in order to understand a bit of what is happening "under the hood".

Error model

The extension methods in the IEasyUAFileTransferExtension2 Class (e.g. those that open or create streams, or those that read or write the full file contents) indicate errors by throwing IOException and its derivations (such as FileNotFoundException) only (this is, leaving aside usage errors such as invalid inputs arguments, which can be prevented, and cannot happen in a properly written code). You can therefore simply "catch" the IOException around these methods calls, and handle it appropriately.

The error model for properties and methods on the actual OPC UA data streams is given by whatever Microsoft prescribes in the Stream Class documentation. Typically it is only the IOException (and its derivations) again that can be thrown (leaving aside usage errors), but you should check the documentation for every operation called individually.

Opening or creating a stream

Before you can read from an OPC UA file stream or write to it, you need to obtain the stream object by opening an existing OPC UA file, or creating a new one as part of the process. Following methods (some with various overloads) are available for that:

All these methods return a stream object that can subsequently be used to access the file data. Eventually, the stream object should be disposed of using the IDisposable.Dispose Method.

Reading, writing, positioning

The actual reading from OPC UA file streams and writing to OPC UA file streams is done by methods on the Stream Class in precisely the same way as with any other type of stream. Here is what you are going to use most:

The example below shows how to read different sections from an OPC UA file stream.

// Shows how to read different sections from an OPC UA file stream.
// Note: Consider using a higher-level abstraction, OPC UA file provider, instead.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

using System;
using System.IO;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.FileTransfer;
using OpcLabs.EasyOpc.UA.IO.Extensions;

namespace UADocExamples.FileTransfer._UAFileStream
{
    class ReadAndSeek
    {
        public static void Main1()
        {
            // Unified Automation .NET based demo server (UaNETServer/UaServerNET.exe)
            UAEndpointDescriptor endpointDescriptor = "opc.tcp://localhost:48030";

            // A node that represents an instance of OPC UA FileType object.
            UANodeDescriptor fileNodeDescriptor = "nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files.TextFile";
            
            // Instantiate the file transfer client object
            var fileTransferClient = new EasyUAFileTransferClient();

            try
            {
                // Get a stream object that corresponds to an OPC UA file.
                Console.WriteLine("Opening the file for reading...");
                using (Stream stream = fileTransferClient.OpenStream(endpointDescriptor, fileNodeDescriptor))
                {
                    // The OPC UA file stream object behaves like any other stream in .NET.

                    Console.WriteLine("Reading first section of a stream...");
                    var buffer1 = new byte[16];
                    int bytesRead1 = stream.Read(buffer1, 0, buffer1.Length);
                    Console.WriteLine($"{bytesRead1} bytes read, buffer: {BitConverter.ToString(buffer1)}");

                    Console.WriteLine("Reading second section of a stream...");
                    var buffer2 = new byte[10];
                    int bytesRead2 = stream.Read(buffer2, 0, buffer2.Length);
                    Console.WriteLine($"{bytesRead2} bytes read, buffer: {BitConverter.ToString(buffer2)}");

                    Console.WriteLine("Seeking...");
                    stream.Seek(100, SeekOrigin.Begin);

                    Console.WriteLine("Reading third section of a stream...");
                    var buffer3 = new byte[20];
                    int bytesRead3 = stream.Read(buffer3, 0, buffer3.Length);
                    Console.WriteLine($"{bytesRead3} bytes read, buffer: {BitConverter.ToString(buffer3)}");
                }
            }
            // OPC UA errors encountered during opening of a UA file stream and operations on such stream are transformed
            // to IOException-s.
            catch (IOException ioException)
            {
                Console.WriteLine("*** Failure: {0}", ioException.GetBaseException().Message);
                return;
            }

            Console.WriteLine();
            Console.WriteLine("Finished...");
        }
    }
}
# Shows how to read different sections from an OPC UA file stream.
# Note: Consider using a higher-level abstraction, OPC UA file provider, instead.
#
# Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
# OPC client and subscriber examples in Python on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-Python .
# The QuickOPC package is needed. Install it using "pip install opclabs_quickopc".
import opclabs_quickopc

# Import .NET namespaces.
from System import *
from System.IO import *
from OpcLabs.EasyOpc.UA import *
from OpcLabs.EasyOpc.UA.FileTransfer import *
from OpcLabs.EasyOpc.UA.IO.Extensions import *
from OpcLabs.EasyOpc.UA.Navigation import *


# Unified Automation .NET based demo server (UaNETServer/UaServerNET.exe).
endpointDescriptor = UAEndpointDescriptor('opc.tcp://localhost:48030')

# A node that represents an instance of OPC UA FileType object.
fileNodeDescriptor = UANodeDescriptor('nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files.TextFile')

# Instantiate the file transfer client object.
fileTransferClient = EasyUAFileTransferClient()

stream = None
try:
    # Get a stream object that corresponds to an OPC UA file.
    print('Opening the file for reading...')
    stream = IEasyUAFileTransferExtension2.OpenStream(fileTransferClient,
                                                      endpointDescriptor,
                                                      UANamedNodeDescriptor(fileNodeDescriptor))

    # The OPC UA file stream object behaves like any other stream in .NET.

    print('Reading first section of a stream...')
    buffer1 = Array.CreateInstance(Byte, 16)
    bytesRead1, _ = stream.Read(buffer1, 0, buffer1.Length)
    print(bytesRead1, ' bytes read, buffer: ', BitConverter.ToString(buffer1), sep='')

    print('Reading second section of a stream...')
    buffer2 = Array.CreateInstance(Byte, 10)
    bytesRead2, _ = stream.Read(buffer2, 0, buffer2.Length)
    print(bytesRead2, ' bytes read, buffer: ', BitConverter.ToString(buffer2), sep='')

    print('Seeking...')
    stream.Seek(100, SeekOrigin.Begin)

    print('Reading third section of a stream...')
    buffer3 = Array.CreateInstance(Byte, 20)
    bytesRead3, _ = stream.Read(buffer3, 0, buffer3.Length)
    print(bytesRead3, ' bytes read, buffer: ', BitConverter.ToString(buffer3), sep='')

# OPC UA errors encountered during opening of a UA file stream and operations on such stream are transformed
# to IOException-s.
except IOException as ioException:
    print('*** Failure: ' + ioException.GetBaseException().Message)
    exit()

finally:
    stream and stream.Dispose()

print()
print('Finished.')
' Shows how to read different sections from an OPC UA file stream.
' Note: Consider using a higher-level abstraction, OPC UA file provider, instead.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

Imports System.IO
Imports OpcLabs.EasyOpc.UA
Imports OpcLabs.EasyOpc.UA.FileTransfer
Imports OpcLabs.EasyOpc.UA.IO.Extensions

Namespace FileTransfer._UAFileStream

    Friend Class ReadAndSeek

        Public Shared Sub Main1()

            ' Unified Automation .NET based demo server (UaNETServer/UaServerNET.exe)
            Dim endpointDescriptor As UAEndpointDescriptor = "opc.tcp://localhost:48030"

            ' A node that represents an instance of OPC UA FileType object.
            Dim fileNodeDescriptor As UANodeDescriptor = "nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files.TextFile"

            ' Instantiate the file transfer client object
            Dim fileTransferClient = New EasyUAFileTransferClient

            Try
                ' Get a stream object that corresponds to an OPC UA file.
                Console.WriteLine("Opening the file for reading...")
                Using stream As Stream = fileTransferClient.OpenStream(endpointDescriptor, fileNodeDescriptor)
                    ' Get a stream object that corresponds to an OPC UA file.
                    Console.WriteLine("Reading first section of a stream...")
                    Dim buffer1(16 - 1) As Byte
                    Dim bytesRead1 As Integer = stream.Read(buffer1, 0, buffer1.Length)
                    Console.WriteLine($"{bytesRead1} bytes read, buffer: {BitConverter.ToString(buffer1)}")

                    Console.WriteLine("Reading second section of a stream...")
                    Dim buffer2(10 - 1) As Byte
                    Dim bytesRead2 As Integer = stream.Read(buffer2, 0, buffer2.Length)
                    Console.WriteLine($"{bytesRead2} bytes read, buffer: {BitConverter.ToString(buffer2)}")

                    Console.WriteLine("Seeking...")
                    stream.Seek(100, SeekOrigin.Begin)

                    Console.WriteLine("Reading third section of a stream...")
                    Dim buffer3(20 - 1) As Byte
                    Dim bytesRead3 As Integer = stream.Read(buffer3, 0, buffer3.Length)
                    Console.WriteLine($"{bytesRead3} bytes read, buffer: {BitConverter.ToString(buffer3)}")
                End Using
                ' OPC UA errors encountered during opening of an UA file stream And operations on such stream are transformed
                ' to IOException-s.
            Catch ioException As IOException
                Console.WriteLine("*** Failure: {0}", ioException.GetBaseException.Message)
                Exit Sub
            End Try

            Console.WriteLine()
            Console.WriteLine("Finished...")
        End Sub
    End Class
End Namespace

 

Full contents 

Some tasks do not require you to work with the file in parts. You may just want to read the whole contents of a file into memory and process it from there. Or, you already have prepared an "image" of the file contents in memory, and just want to create a file that contains the data you have created.

In these cases, you can take a shortcut, and use extension methods designed for that purpose. Internally, the methods are implemented using OPC UA file streams, and share the same internals, and error model, with them. They are:

The example below shows how to read the full contents of an OPC UA file at once, using the file transfer client.

// Shows how to read the full contents of an OPC UA file at once, using the file transfer client.
// Note: Consider using a higher-level abstraction, OPC UA file provider, instead.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

using System;
using System.IO;
using System.Text;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.FileTransfer;
using OpcLabs.EasyOpc.UA.IO.Extensions;

namespace UADocExamples.FileTransfer._EasyUAFileTransferClient
{
    class ReadAllBytes
    {
        public static void Main1()
        {
            // Unified Automation .NET based demo server (UaNETServer/UaServerNET.exe)
            UAEndpointDescriptor endpointDescriptor = "opc.tcp://localhost:48030";

            // A node that represents an instance of OPC UA FileType object.
            UANodeDescriptor fileNodeDescriptor = "nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files.TextFile";
            
            // Instantiate the file transfer client object
            var fileTransferClient = new EasyUAFileTransferClient();

            // Read in all contents from a specified file node.
            byte[] bytes;
            try
            {
                Console.WriteLine("Reading the whole file...");
                bytes = fileTransferClient.ReadAllBytes(endpointDescriptor, fileNodeDescriptor);
            }
            // Beware that ReadAllFileBytes throws IOException and not UAException.
            catch (IOException ioException)
            {
                Console.WriteLine("*** Failure: {0}", ioException.GetBaseException().Message);
                return;
            }

            // Display result
            Console.WriteLine();
            // We know that the file contains text, so we convert the received data to a string. If the file contents was
            // binary, you would process the data according to their format.
            string text = Encoding.UTF8.GetString(bytes);
            Console.WriteLine("File content:");
            Console.WriteLine(text);

            Console.WriteLine();
            Console.WriteLine("Finished...");
        }
    }
}
# Shows how to read the full contents of an OPC UA file at once, using the file transfer client.
# Note: Consider using a higher-level abstraction, OPC UA file provider, instead.
#
# Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
# OPC client and subscriber examples in Python on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-Python .
# The QuickOPC package is needed. Install it using "pip install opclabs_quickopc".
import opclabs_quickopc

# Import .NET namespaces.
from System.Text import *
from OpcLabs.EasyOpc.UA import *
from OpcLabs.EasyOpc.UA.FileTransfer import *
from OpcLabs.EasyOpc.UA.IO.Extensions import *
from OpcLabs.EasyOpc.UA.Navigation import *


# Unified Automation .NET based demo server (UaNETServer/UaServerNET.exe).
endpointDescriptor = UAEndpointDescriptor('opc.tcp://localhost:48030')

# A node that represents an instance of OPC UA FileType object.
fileNodeDescriptor = UANodeDescriptor('nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files.TextFile')

# Instantiate the file transfer client object.
fileTransferClient = EasyUAFileTransferClient()

# Read in all contents from a specified file node.
try:
    print('Reading the whole file...')
    bytes = IEasyUAFileTransferExtension2.ReadAllBytes(fileTransferClient,
                                                       endpointDescriptor,
                                                       UANamedNodeDescriptor(fileNodeDescriptor))

# Beware that ReadAllFileBytes throws IOException and not UAException.
except IOException as ioException:
    print('*** Failure: ' + ioException.GetBaseException().Message)
    exit()

# Display result.
print()
# We know that the file contains text, so we convert the received data to a string. If the file contents was
# binary, you would process the data according to their format.
text = Encoding.UTF8.GetString(bytes)
print('File content:')
print(text)

print()
print('Finished.')
' Shows how to read the full contents of an OPC UA file at once, using the file transfer client.
' Note: Consider using a higher-level abstraction, OPC UA file provider, instead.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

Imports System.IO
Imports System.Text
Imports OpcLabs.EasyOpc.UA
Imports OpcLabs.EasyOpc.UA.FileTransfer
Imports OpcLabs.EasyOpc.UA.IO.Extensions

Namespace FileTransfer._EasyUAFileTransferClient

    Friend Class ReadAllBytes

        Public Shared Sub Main1()

            ' Unified Automation .NET based demo server (UaNETServer/UaServerNET.exe)
            Dim endpointDescriptor As UAEndpointDescriptor = "opc.tcp://localhost:48030"

            ' A node that represents an instance of OPC UA FileType object.
            Dim fileNodeDescriptor As UANodeDescriptor = "nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files.TextFile"

            ' Instantiate the file transfer client object
            Dim fileTransferClient = New EasyUAFileTransferClient

            ' Read in all contents from a specified file node.
            Dim bytes As Byte()
            Try
                Console.WriteLine("Reading the whole file...")
                bytes = fileTransferClient.ReadAllBytes(endpointDescriptor, fileNodeDescriptor)

                ' Beware that ReadAllFileBytes throws IOException And Not UAException.
            Catch ioException As IOException
                Console.WriteLine("*** Failure: {0}", ioException.GetBaseException.Message)
                Exit Sub
            End Try

            ' Display result
            Console.WriteLine()
            ' We know that the file contains text, so we convert the received data to a string. If the file contents was
            ' binary, you would process the data according to their format.
            Dim text As String = Encoding.UTF8.GetString(bytes)
            Console.WriteLine("File content:")
            Console.WriteLine(text)

            Console.WriteLine()
            Console.WriteLine("Finished...")
        End Sub
    End Class
End Namespace

 

The example below shows how to write the full contents of an OPC UA file at once, using the file transfer client.

// Shows how to write the full contents of an OPC UA file at once, using the file transfer client.
// Note: Consider using a higher-level abstraction, OPC UA file provider, instead.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

using System;
using System.IO;
using System.Text;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.FileTransfer;
using OpcLabs.EasyOpc.UA.IO.Extensions;

namespace UADocExamples.FileTransfer._EasyUAFileTransferClient
{
    class WriteAllBytes
    {
        public static void Main1()
        {
            // Unified Automation .NET based demo server (UaNETServer/UaServerNET.exe)
            UAEndpointDescriptor endpointDescriptor = "opc.tcp://localhost:48030";

            // A node that represents an instance of OPC UA FileType object.
            UANodeDescriptor fileNodeDescriptor = "nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files.TextFile";
            
            // Instantiate the file transfer client object
            var fileTransferClient = new EasyUAFileTransferClient();

            // Write all contents into a specified file node.
            byte[] bytes = Encoding.UTF8.GetBytes("TEXT FROM FILE TRANSFER CLIENT EXAMPLE. Demonstrates writing the whole contents of a file at once.");
            try
            {
                Console.WriteLine("Writing the whole file...");
                fileTransferClient.WriteAllBytes(endpointDescriptor, fileNodeDescriptor, bytes);

                // Due to an issue in the server, the file might not be readable now, without server restart.
                //Console.WriteLine("Reading the data back...");
                //byte[] data = fileTransferClient.ReadAllFileBytes(endpointDescriptor, fileNodeDescriptor);
                //Console.WriteLine(Encoding.UTF8.GetString(data));
            }
            // Beware that WriteAllFileBytes throws IOException and not UAException.
            catch (IOException ioException)
            {
                Console.WriteLine("*** Failure: {0}", ioException.GetBaseException().Message);
                return;
            }

            Console.WriteLine();
            Console.WriteLine("Finished...");
        }
    }
}
# Shows how to write the full contents of an OPC UA file at once, using the file transfer client.
# Note: Consider using a higher-level abstraction, OPC UA file provider, instead.
#
# Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
# OPC client and subscriber examples in Python on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-Python .
# The QuickOPC package is needed. Install it using "pip install opclabs_quickopc".
import opclabs_quickopc

# Import .NET namespaces.
from System.Text import *
from OpcLabs.EasyOpc.UA import *
from OpcLabs.EasyOpc.UA.Extensions import *
from OpcLabs.EasyOpc.UA.FileTransfer import *
from OpcLabs.EasyOpc.UA.IO.Extensions import *
from OpcLabs.EasyOpc.UA.Navigation import *


# Unified Automation .NET based demo server (UaNETServer/UaServerNET.exe).
endpointDescriptor = UAEndpointDescriptor('opc.tcp://localhost:48030')
endpointDescriptor = UAEndpointDescriptorExtension.WithUserNameIdentity(endpointDescriptor,'john', 'master')

# A node that represents an instance of OPC UA FileType object.
fileNodeDescriptor = UANodeDescriptor('nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files.TextFile')

# Instantiate the file transfer client object.
fileTransferClient = EasyUAFileTransferClient()

# Prevent prompt to trust the server certificate (INSECURE, used just for smooth example flow).
EasyUAClient.SharedParameters.EngineParameters.CertificateAcceptancePolicy.TrustEndpointUrlString(
    endpointDescriptor.UrlString)

# Write all contents into a specified file node.
bytes = Encoding.UTF8.GetBytes('TEXT FROM FILE TRANSFER CLIENT EXAMPLE. Demonstrates writing the whole contents of a '
                               'file at once.')
try:
    print('Writing the whole file...')
    IEasyUAFileTransferExtension2.WriteAllBytes(fileTransferClient,
                                                endpointDescriptor,
                                                UANamedNodeDescriptor(fileNodeDescriptor),
                                                bytes)

    # Due to an issue in the server, the file might not be readable now, without server restart.
    print('Reading the data back...')
    data = IEasyUAFileTransferExtension2.ReadAllBytes(fileTransferClient,
                                                       endpointDescriptor,
                                                       UANamedNodeDescriptor(fileNodeDescriptor))
    print(Encoding.UTF8.GetString(data))

# Beware that ReadAllFileBytes throws IOException and not UAException.
except IOException as ioException:
    print('*** Failure: ' + ioException.GetBaseException().Message)
    exit()

print()
print('Finished.')
' Shows how to write the full contents of an OPC UA file at once, using the file transfer client.
' Note: Consider using a higher-level abstraction, OPC UA file provider, instead.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

Imports System.IO
Imports System.Text
Imports OpcLabs.EasyOpc.UA
Imports OpcLabs.EasyOpc.UA.FileTransfer
Imports OpcLabs.EasyOpc.UA.IO.Extensions

Namespace FileTransfer._EasyUAFileTransferClient

    Friend Class WriteAllBytes

        Public Shared Sub Main1()

            ' Unified Automation .NET based demo server (UaNETServer/UaServerNET.exe)
            Dim endpointDescriptor As UAEndpointDescriptor = "opc.tcp://localhost:48030"

            ' A node that represents an instance of OPC UA FileType object.
            Dim fileNodeDescriptor As UANodeDescriptor = "nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files.TextFile"

            ' Instantiate the file transfer client object
            Dim fileTransferClient = New EasyUAFileTransferClient

            ' Write all contents into a specified file node.
            Dim bytes As Byte() = Encoding.UTF8.GetBytes("TEXT FROM FILE TRANSFER CLIENT EXAMPLE. Demonstrates writing the whole contents of a file at once.")
            Try
                Console.WriteLine("Writing the whole file...")
                fileTransferClient.WriteAllBytes(endpointDescriptor, fileNodeDescriptor, bytes)

                ' Due to an issue in the server, the file might Not be readable now, without server restart.
                'Console.WriteLine("Reading the data back...")
                'Dim data As Byte() = fileTransferClient.ReadAllFileBytes(endpointDescriptor, fileNodeDescriptor)
                'Console.WriteLine(Encoding.UTF8.GetString(data))

                ' Beware that WriteAllFileBytes throws IOException And Not UAException.
            Catch ioException As IOException
                Console.WriteLine("*** Failure: {0}", ioException.GetBaseException.Message)
                Exit Sub
            End Try

            Console.WriteLine()
            Console.WriteLine("Finished...")
        End Sub
    End Class
End Namespace

 

Readers and writers

For reading and writing primitive data types as binary values in a specific encoding, or reading and writing sequential series of characters (text), Microsoft .NET contains "reader" and "writer" classes which make these tasks easier. Examples of these classes are:

All these classes operate on top of existing streams. It can be any type of stream, and OPC UA file stream work with the "readers" and "writers" just fine.

You can create all above "readers" and "writers" using their constructors and passing them an already existing OPC UA file stream, but OPC Data Client also gives you some methods to create them directly:

The example below shows how to open an OPC UA file stream for reading, and read its content using a text reader object.

// Shows how to open an OPC UA file stream for reading, and read its content using a text reader object.
// Note: Consider using a higher-level abstraction, OPC UA file provider, instead.
//
// Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

using System;
using System.IO;
using OpcLabs.EasyOpc.UA;
using OpcLabs.EasyOpc.UA.FileTransfer;
using OpcLabs.EasyOpc.UA.IO.Extensions;

namespace UADocExamples.FileTransfer._UAFileStream
{
    class ReadText
    {
        public static void Main1()
        {
            // Unified Automation .NET based demo server (UaNETServer/UaServerNET.exe)
            UAEndpointDescriptor endpointDescriptor = "opc.tcp://localhost:48030";

            // A node that represents an instance of OPC UA FileType object.
            UANodeDescriptor fileNodeDescriptor = "nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files.TextFile";
            
            // Instantiate the file transfer client object
            var fileTransferClient = new EasyUAFileTransferClient();

            try
            {
                // Get a stream reader object that corresponds to an OPC UA file.
                Console.WriteLine("Opening the file for reading...");

                // We know that the file contains text, so we read it using a stream reader. If the file content was
                // binary, you would process the stream according to the data format.
                using (StreamReader streamReader = fileTransferClient.OpenStreamReader(endpointDescriptor, fileNodeDescriptor))
                {
                    // The OPC UA stream reader object behaves like any other stream reader in .NET.

                    // Read in the text from the file and display it line by line.
                    Console.WriteLine();
                    Console.WriteLine("Reading text lines:");
                    int i = 0;
                    while (!streamReader.EndOfStream)
                    {
                        string line = streamReader.ReadLine();
                        Console.WriteLine($"[{i}] {line}");
                        i++;
                    }
                }
            }
            // OPC UA errors encountered during opening of a UA file stream and operations on such stream are transformed
            // to IOException-s.
            catch (IOException ioException)
            {
                Console.WriteLine("*** Failure: {0}", ioException.GetBaseException().Message);
                return;
            }

            Console.WriteLine();
            Console.WriteLine("Finished...");
        }
    }
}
# Shows how to read different sections from an OPC UA file stream.
# Note: Consider using a higher-level abstraction, OPC UA file provider, instead.
#
# Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .
# OPC client and subscriber examples in Python on GitHub: https://github.com/OPCLabs/Examples-QuickOPC-Python .
# The QuickOPC package is needed. Install it using "pip install opclabs_quickopc".
import opclabs_quickopc

# Import .NET namespaces.
from OpcLabs.EasyOpc.UA import *
from OpcLabs.EasyOpc.UA.FileTransfer import *
from OpcLabs.EasyOpc.UA.IO.Extensions import *
from OpcLabs.EasyOpc.UA.Navigation import *


# Unified Automation .NET based demo server (UaNETServer/UaServerNET.exe).
endpointDescriptor = UAEndpointDescriptor('opc.tcp://localhost:48030')

# A node that represents an instance of OPC UA FileType object.
fileNodeDescriptor = UANodeDescriptor('nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files.TextFile')

# Instantiate the file transfer client object.
fileTransferClient = EasyUAFileTransferClient()

streamReader = None
try:
    # Get a stream reader object that corresponds to an OPC UA file.
    print('Opening the file for reading...')

    # We know that the file contains text, so we read it using a stream reader. If the file content was
    # binary, you would process the stream according to the data format.
    streamReader = IEasyUAFileTransferExtension2.OpenStreamReader(fileTransferClient,
                                                                  endpointDescriptor,
                                                                  UANamedNodeDescriptor(fileNodeDescriptor))

    # The OPC UA stream reader object behaves like any other stream reader in .NET.

    # Read in the text from the file and display it line by line.
    print()
    print('Reading text lines...')
    i = 0
    while not streamReader.EndOfStream:
        line = streamReader.ReadLine()
        print('[', i, '] ', line, sep='')
        i = i + 1

# OPC UA errors encountered during opening of a UA file stream and operations on such stream are transformed
# to IOException-s.
except IOException as ioException:
    print('*** Failure: ' + ioException.GetBaseException().Message)
    exit()

finally:
    streamReader and streamReader.Dispose()

print()
print('Finished.')
' Shows how to open an OPC UA file stream for reading, and read its content using a text reader object.
' Note: Consider using a higher-level abstraction, OPC UA file provider, instead.
'
' Find all latest examples here: https://opclabs.doc-that.com/files/onlinedocs/OPCLabs-OpcStudio/Latest/examples.html .

Imports System.IO
Imports OpcLabs.EasyOpc.UA
Imports OpcLabs.EasyOpc.UA.FileTransfer
Imports OpcLabs.EasyOpc.UA.IO.Extensions

Namespace FileTransfer._UAFileStream

    Friend Class ReadText

        Public Shared Sub Main1()

            ' Unified Automation .NET based demo server (UaNETServer/UaServerNET.exe)
            Dim endpointDescriptor As UAEndpointDescriptor = "opc.tcp://localhost:48030"

            ' A node that represents an instance of OPC UA FileType object.
            Dim fileNodeDescriptor As UANodeDescriptor = "nsu=http://www.unifiedautomation.com/DemoServer/ ;s=Demo.Files.TextFile"

            ' Instantiate the file transfer client object
            Dim fileTransferClient = New EasyUAFileTransferClient

            Try
                ' Get a stream object that corresponds to an OPC UA file.
                Console.WriteLine("Opening the file for reading...")

                ' We know that the file contains text, so we read it using a stream reader. If the file content was
                ' binary, you would process the stream according to the data format.
                Using streamReader As StreamReader = fileTransferClient.OpenStreamReader(endpointDescriptor, fileNodeDescriptor)
                    ' The OPC UA stream reader object behaves like any other stream reader in .NET.

                    ' Read in the text from the file and display it line by line.
                    Console.WriteLine()
                    Console.WriteLine("Reading text lines:")
                    Dim i As Integer = 0
                    While Not streamReader.EndOfStream
                        Dim line As String = streamReader.ReadLine()
                        Console.WriteLine($"[{i}] {line}")
                        i += 1
                    End While
                End Using
                ' OPC UA errors encountered during opening of an UA file stream And operations on such stream are transformed
                ' to IOException-s.
            Catch ioException As IOException
                Console.WriteLine("*** Failure: {0}", ioException.GetBaseException.Message)
                Exit Sub
            End Try

            Console.WriteLine()
            Console.WriteLine("Finished...")
        End Sub
    End Class
End Namespace

 

See Also

Examples - OPC UA File Transfer

Knowledge Base